//
//  JTPlayerView.swift
//  JTPlayerView
//
//  Created by green on 16/4/23.
//  Copyright © 2016年 CocoaPods. All rights reserved.
//

import UIKit
import UtoVRPlayer

// 播放器视图
class JTPlayerView: UIView {

    private weak var player: UVPlayer!
    
    weak var delegate: JTPlayerViewDelegate? {
        
        didSet {
            
            reset()
        }
    }
    
    // MARK: - Life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setup()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setup()
    }
    
    deinit {
        
        print("\(self) deinit ")
        destroy()
    }
    
    // MARK: - Private
    
    private func setup() {
        
        // 设置播放器
        reset()
    }
    
    /**
     * 竖屏返回事件
     */
    @objc private func portraitBack() {
        
        delegate?.portraitBack?(self)
    }
    
    /**
     * 横屏返回事件。
     */
    @objc private func horizontalBack() {
        
        delegate?.horizontalBack?(self)
    }
    
    // MARK: - Public
    
    /**
     重置
     */
    func reset() {
        
        if player != nil {
            
            destroy()
        }
        
        player = UVPlayer(configuration: nil)
        addSubview(player.playerView)
        
        player.playerView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[playerView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["playerView":player.playerView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[playerView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["playerView":player.playerView]))
        
        if let delegate = delegate {
            
            if delegate.respondsToSelector(#selector(JTPlayerViewDelegate.horizontalBack(_:))) {
                
                if delegate.respondsToSelector(#selector(JTPlayerViewDelegate.horizontalBackIsValid)) {
                    
                    if delegate.horizontalBackIsValid!() {
                        
                        player.setHorizontalBackButtonTarget(self, selector: #selector(JTPlayerView.horizontalBack))
                    }
                } else {
                    
                    player.setHorizontalBackButtonTarget(self, selector: #selector(JTPlayerView.horizontalBack))
                }
            }
            if delegate.respondsToSelector(#selector(JTPlayerViewDelegate.portraitBack(_:))) {
                
                player.setPortraitBackButtonTarget(self, selector: #selector(JTPlayerView.portraitBack))
            }
        }
    }
    
    /**
     播放
     */
    func play(item: UVPlayerItem) {
        
        player.clearItems()
        player.appendItem(item)
    }
    
    /**
     暂停
     */
    func pause() {
        
        player.pause()
    }
    
    /**
     恢复
     */
    func resume() {
        
        player.play()
    }
    
    /**
     摧毁
     */
    func destroy() {
        
        player.prepareToRelease()
    }
    
    /**
     * 设置横屏时的标题
     */
    func setTitleText(title: String!) {
        
        player.setTitleText(title)
    }
    
}
